package com.aduerr.v1shouyin;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import com.aduerr.v1shouyin.common.Constants;
import com.aduerr.v1shouyin.di.component.AppComponent;
import com.aduerr.v1shouyin.di.component.DaggerAppComponent;
import com.aduerr.v1shouyin.di.module.AppModule;
import com.blankj.utilcode.util.Utils;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.orhanobut.hawk.Hawk;
import com.tencent.bugly.crashreport.CrashReport;
import me.jessyan.autosize.AutoSize;
import me.jessyan.autosize.AutoSizeConfig;
import me.jessyan.autosize.unit.Subunits;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * 类名: App
 * 此类用途: ---
 *
 * @Date: 2017-08-08 17:57
 * @Email: sage.lu6@gmail.com
 * @FileName: com.aduer.shouyin.App.java
 */
public class App extends Application {
    private static App app;
    private AppComponent mAppComponent;
    //    public static HttpsUtils.SSLParams sslParams;
    private boolean isDebug = false;

    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if(!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch(Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if(reader != null) {
                    reader.close();
                }
            } catch(IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }

    /*private void initLeakCanary() {
        if (LeakCanary.isInAnalyzerProcess(this)) {
            return;
        }
        LeakCanary.install(this);
    }*/

    public static App getApp() {
        return app;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        /*    *//*开启网络广播监听*//*
        NetStateReceiver.registerNetworkStateReceiver(this);*/
        //new InputStream[]{getResources().openRawResource(R.raw.srca)}
//        sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
        Hawk.init(this).build();
//        ZXingLibrary.initDisplayOpinion(this);
        mAppComponent = DaggerAppComponent.builder().appModule(new AppModule(this,
                Constants.BASE_URL)).build();
        app = this;
        Utils.init(app);

        /* autosize 屏幕适配配置 */
        AutoSizeConfig.getInstance()
                .setLog(false)
                .getUnitsManager()
                .setSupportDP(false)
                .setSupportSP(false)
                .setSupportSubunits(Subunits.MM);
        AutoSize.initCompatMultiProcess(this);


        //极光推送
   /*     JPushInterface.setDebugMode(false);
        JPushInterface.init(this);*/
        //科大讯飞的集成
        SpeechUtility.createUtility(this, SpeechConstant.APPID + "=58cb3d29");

//        initBugly(); //初步怀疑 这个在pos机上会造成ANR，所以注释掉 --鹿鸣

//        initLeakCanary();

       /* StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectAll()//监测所有内容
                .penaltyLog()//违规对log日志
                .penaltyDialog()//违规提示
                .build());

        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectAll()//监测所以内容
                .penaltyLog()//违规对log日志
                .penaltyDeath()//违规Crash
                .build());*/
    }

    private void initBugly() {
        Context context = getApplicationContext();
// 获取当前包名
        String packageName = context.getPackageName();
// 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
// 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
// 初始化Bugly
        CrashReport.initCrashReport(context, "1105451851", isDebug, strategy);
    }

   /* @Override
    public void onLowMemory() {
        super.onLowMemory();
        NetStateReceiver.unRegisterNetworkStateReceiver(this);
        android.os.Process.killProcess(android.os.Process.myPid());
    }*/

    public AppComponent getAppCommponent() {
        return mAppComponent;
    }
}
